
from django.shortcuts import render
from book.models import Book
from utils.LoginForm import LoginForm
from author.author_utils.AuthorForm import AuthorForm
from author.models import Author, AuthorDetail, AuthorLogin
from publish.models import Publish
from utils.MyPage import Pagination


def main(request):
    return render(request, template_name='author_main.html')


def register(request):
    login_form = LoginForm()
    author_form = AuthorForm()
    publish_all = Publish.objects.all()
    if request.method == 'POST':
        data = request.POST
        login_form = LoginForm(data)
        login_form.fields['username'].validators[1].table = 'author'
        author_form = AuthorForm(data)
        is_identity_login = login_form.is_valid()
        is_identity_author = author_form.is_valid()
        if is_identity_login and is_identity_author:
            author_login, author_info, author_detail = handle_data(data)
            author_detail_obj = AuthorDetail.objects.create(**author_detail)
            print(author_detail_obj)
            author_login_obj = AuthorLogin.objects.create(**author_login)
            author_info_obj = Author.objects.create(**author_info, author_detail=author_detail_obj,
                                                    author_login=author_login_obj)
            publish_id = data.get('publish_id')
            if publish_id:
                author_info_obj.set(publish_id=publish_id)
            return render(request, 'author_main.html')
    return render(request, 'author_register.html', locals())


def handle_data(data):
    author_login = {
        'username': data.get('username'),
        'password': data.get('password'),
    }
    author_info = {
        'name': data.get('name'),
        'age': data.get('age'),
        'gender': data.get('gender'),
    }
    author_detail = {
        'email': data.get('email'),
        'addr': data.get('addr'),
        'desc': data.get('desc')
    }
    return author_login, author_info, author_detail


def check_book(request):
    sign = request.COOKIES.get('sign')
    if not sign:
        return render(request, 'author_no_cookies.html')
    author_name = sign.get('username')
    current_page = request.GET.get('page', 1)
    book_data_all = Book.objects.filter(author__name=author_name)
    all_count = book_data_all.count()
    page_obj = Pagination(current_page=current_page, all_count=all_count, per_page_num=5, pager_count=5)
    book_data = book_data_all[page_obj.start:page_obj.end]
    page_html = page_obj.page_html()
    return render(request, 'author_book.html', context=locals())


def check_publish(request):
    current_page = request.GET.get('page', 1)
    publish_obj_all = Publish.objects.all()
    all_count = publish_obj_all.count()
    page_obj = Pagination(current_page=current_page, all_count=all_count, per_page_num=5, pager_count=5)
    publish_obj = publish_obj_all[page_obj.start:page_obj.end]
    page_html = page_obj.page_html()
    return render(request, 'author_publish.html', locals())
